#include "../includes/torreMalditona.h"

torreMalditona::torreMalditona()
{
	
	this->ringCount = 3;
	
	this->resetPins();
	
}

torreMalditona::torreMalditona( unsigned short int _ringCount )
{
	this->ringCount = _ringCount;
	
	this->resetPins();
} 

torreMalditona::~torreMalditona()
{
}

void
torreMalditona::moveRing( unsigned short int _origin , unsigned short int _destination )
{
	switch ( _origin )
	{
	case 1:
		this->insertRing( this->firstPin.getInfo() , _destination );
		this->firstPin.pop();
		break;
	case 2:
		this->insertRing( this->secondPin.getInfo() , _destination );
		this->secondPin.pop();
		break;
	case 3:
		this->insertRing( this->thirdPin.getInfo() , _destination );
		this->thirdPin.pop();
		break;
	}
}

void
torreMalditona::restartGame( unsigned short int _ringCount = 0 )
{
	this->ringCount = _ringCount;
	
	this->resetPins();
}

std::vector<unsigned short int>
torreMalditona::getPinStats( unsigned short int pin )
{
	switch( pin )
	{
	case 1:
		return this->firstPin.list();
	case 2:
		return this->secondPin.list();
	case 3:
		return this->thirdPin.list();
	default:
		this->throwException("MALDITO! ESTE PINO NAO EXISTE!");
	}
}

bool
torreMalditona::checkWin()
{
	if( this->firstPin.isEmpty() && this->secondPin.isEmpty() )
	{
		return true;
	}
	else
	{
		return false;
	}
}

/*****************************
 * 
 * 
 * AQUI � PRIVADO
 * N�O VAI SUBI NINGU�M!
 * 
 * 
 *****************************/

void
torreMalditona::resetPins()
{
	this->firstPin.clear();
	this->secondPin.clear();
	this->thirdPin.clear();
	
	for( unsigned short int count = this->ringCount ; count > 0 ; count-- )
	{
		this->firstPin.push( count );
	}
}

void
torreMalditona::insertRing( const unsigned short int _info , const unsigned short int _destination )
{
	switch ( _destination )
	{
	case 1:
		if ( this->firstPin.isEmpty() || this->firstPin.getInfo() > _info )
			this->firstPin.push( _info );
		else
		{
			this->throwException( "naum da pra jogar esse anel no pino 1" );
		}
		break;
	case 2:
		if ( this->secondPin.isEmpty() || this->secondPin.getInfo() > _info )
			this->secondPin.push( _info );
		else
		{
			this->throwException( "naum da pra jogar esse anel no pino 2" );
		 }
		break;
	case 3:
		if( this->thirdPin.isEmpty() || this->thirdPin.getInfo() > _info )
			this->thirdPin.push( _info );
		else
		{
			this->throwException( "naum da pra jogar esse anel no pino 3" );
		}
		break;
	default:
		this->throwException( "maldição dos pênis empalados! num tem esse pino!!!" );
		break;
	}
}

void
torreMalditona::throwException( const std::string msg )
{
	error*
	e = new error( msg );
	
	throw e;
}
